package com.cxg.fairy.ec.detail;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.cxg.fairy.ui.recycler.MultipleItemEntity;
import com.cxg.fairy.util.log.LatteLogger;
import com.cxg.fairy.util.money.MoneyUtil;
import com.diabin.latte.ec.R;
import com.hedgehog.ratingbar.RatingBar;
import com.rengwuxian.materialedittext.MaterialEditText;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/4/10.
 */

public class CommentActionAdapter extends ArrayAdapter{

    private float desStar = 0;

    private float serStar = 0;

    private String remark = "";

    private CommentActionDelegate mCommentActionDelegate = null;

    private static final RequestOptions OPTIONS = new RequestOptions()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .dontAnimate()
            .centerCrop();

    private JSONArray orderCommentArray = new JSONArray();

    private ArrayList<MultipleItemEntity> entityList = new ArrayList<MultipleItemEntity>();

    public CommentActionAdapter(@NonNull Context context, int resource, @NonNull List objects,CommentActionDelegate commentDelegate) {
        super(context, resource, objects);
        mCommentActionDelegate = commentDelegate;
    }

    @NonNull
    @Override
    public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        final MultipleItemEntity entity = (MultipleItemEntity) getItem(position);
        final JSONObject orderCommetnObj = new JSONObject();
        orderCommetnObj.clear();
        final int skuId = entity.getField("skuId");
        final String goodsName = entity.getField("goodsName");
        final double sellPrice = entity.getField("sellPrice");
        final String thumb = entity.getField("thumb");

        LinearLayoutCompat view = (LinearLayoutCompat) LayoutInflater.from(parent.getRootView().getContext()).inflate(R.layout.item_comments_action_list, parent,false);
        RatingBar mRatingBarDesc = (RatingBar) view.findViewById(R.id.rst_comment_start);
        RatingBar mRatingBarServe = (RatingBar) view.findViewById(R.id.rst_comment_service_star);

        AppCompatImageView mGoodsImg = (AppCompatImageView) view.findViewById(R.id.iv_goods_img);
        AppCompatTextView mGoodsName = (AppCompatTextView) view.findViewById(R.id.atv_comment_goods_name);
        AppCompatTextView mGoodsPrice = (AppCompatTextView) view.findViewById(R.id.atv_comment_goods_price);
        final MaterialEditText mGoodsRemark = (MaterialEditText) view.findViewById(R.id.met_new_needs_remark);
        mGoodsRemark.setHideUnderline(true);
        mGoodsRemark.setBackgroundColor(Color.parseColor("#f5f5f5"));

        Glide.with(view)
                .load(thumb)
                .apply(OPTIONS)
                .into(mGoodsImg);
        mGoodsName.setText(goodsName);
        mGoodsPrice.setText("¥"+MoneyUtil.formatMoney(sellPrice));

        mGoodsRemark.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                remark = mGoodsRemark.getText().toString().trim();
                buildJsonArray(orderCommetnObj,position);

                LatteLogger.e("position=",position+"|SIZE="+orderCommentArray.size()+"|TEXT="+orderCommetnObj.get("eval")+"|DESSTAR="+desStar+"|SERSTAR="+serStar);
            }
        });

        orderCommetnObj.put("skuId", skuId);

        mRatingBarDesc.setOnRatingChangeListener(
                new RatingBar.OnRatingChangeListener() {
                    @Override
                    public void onRatingChange(float RatingCount) {
                        desStar = RatingCount;
                        buildJsonArray(orderCommetnObj,position);
                        LatteLogger.e("position=",position+"|SIZE="+orderCommentArray.size()+"|TEXT="+orderCommetnObj.get("eval")+"|DESSTAR="+desStar+"|SERSTAR="+serStar);
                    }
                }
        );

        mRatingBarServe.setOnRatingChangeListener(
                new RatingBar.OnRatingChangeListener() {
                    @Override
                    public void onRatingChange(float RatingCount) {
                        serStar = RatingCount;
                        buildJsonArray(orderCommetnObj,position);
                    }
                }
        );

        return view;
    }

    public JSONArray getOrderCommentArray() {
        return orderCommentArray;
    }

    private void buildJsonArray(JSONObject obj , int position) {
        if (orderCommentArray.size() >= position + 1) {
            if (orderCommentArray.get(position) != null) {
                orderCommentArray.remove(position);
            }
        }
        obj.put("eval", remark);
        obj.put("desStar", desStar);
        obj.put("servStar", serStar);
        orderCommentArray.add(position,obj);
        LatteLogger.e("pos=",position+"|size="+orderCommentArray.size());
    }

}
